/*
 * HtmlUtil.java
 * classes : com.ruint.core.utils.html.HtmlUtil
 * @author ruint
 * V 1.0.0
 * Create at 2014-11-12 下午4:12:52
 */
package com.ruint.core.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * com.ruint.core.utils.html.HtmlUtil
 * 
 * @author ruint <br/>
 *         create at 2014-11-12 下午4:12:52
 */
public class HtmlUtils {
  private final static String regxpForHtml = "<([^>]*)>"; 
  @SuppressWarnings("unused")
  private final static String regxpForImgTag = "<\\s*img\\s+([^>]*)\\s*>"; 
  @SuppressWarnings("unused")
  private final static String regxpForImaTagSrcAttrib = "src=\"([^\"]+)\""; 

  public HtmlUtils() {
  }

  public String replaceTag(String input) {
    if (!hasSpecialChars(input)) {
      return input;
    }
    StringBuffer filtered = new StringBuffer(input.length());
    char c;
    for (int i = 0; i <= input.length() - 1; i++) {
      c = input.charAt(i);
      switch (c) {
        case '<':
          filtered.append("&lt;");
          break;
        case '>':
          filtered.append("&gt;");
          break;
        case '"':
          filtered.append("&quot;");
          break;
        case '&':
          filtered.append("&amp;");
          break;
        default:
          filtered.append(c);
      }

    }
    return (filtered.toString());
  }

  public boolean hasSpecialChars(String input) {
    boolean flag = false;
    if ((input != null) && (input.length() > 0)) {
      char c;
      for (int i = 0; i <= input.length() - 1; i++) {
        c = input.charAt(i);
        switch (c) {
          case '>':
            flag = true;
            break;
          case '<':
            flag = true;
            break;
          case '"':
            flag = true;
            break;
          case '&':
            flag = true;
            break;
        }
      }
    }
    return flag;
  }

  public static String filterHtml(String str) {
    Pattern pattern = Pattern.compile(regxpForHtml);
    Matcher matcher = pattern.matcher(str);
    StringBuffer sb = new StringBuffer();
    boolean result1 = matcher.find();
    while (result1) {
      matcher.appendReplacement(sb, "");
      result1 = matcher.find();
    }
    matcher.appendTail(sb);
    return sb.toString();
  }

  public static String fiterHtmlTag(String str, String tag) {
    String regxp = "<\\s*" + tag + "\\s+([^>]*)\\s*>";
    Pattern pattern = Pattern.compile(regxp);
    Matcher matcher = pattern.matcher(str);
    StringBuffer sb = new StringBuffer();
    boolean result1 = matcher.find();
    while (result1) {
      matcher.appendReplacement(sb, "");
      result1 = matcher.find();
    }
    matcher.appendTail(sb);
    return sb.toString();
  }

  public static String replaceHtmlTag(String str, String beforeTag, String tagAttrib, String startTag, String endTag) {
    String regxpForTag = "<\\s*" + beforeTag + "\\s+([^>]*)\\s*>";
    String regxpForTagAttrib = tagAttrib + "=\"([^\"]+)\"";
    Pattern patternForTag = Pattern.compile(regxpForTag);
    Pattern patternForAttrib = Pattern.compile(regxpForTagAttrib);
    Matcher matcherForTag = patternForTag.matcher(str);
    StringBuffer sb = new StringBuffer();
    boolean result = matcherForTag.find();
    while (result) {
      StringBuffer sbreplace = new StringBuffer();
      Matcher matcherForAttrib = patternForAttrib.matcher(matcherForTag.group(1));
      if (matcherForAttrib.find()) {
        matcherForAttrib.appendReplacement(sbreplace, startTag + matcherForAttrib.group(1) + endTag);
      }
      matcherForTag.appendReplacement(sb, sbreplace.toString());
      result = matcherForTag.find();
    }
    matcherForTag.appendTail(sb);
    return sb.toString();
  }

  /**
   * 
   * @param content
   * @return
   */
  @SuppressWarnings("unused")
  private List<String> getRegexedUrls(String content) {
    String regEx_img = "(http|https)://[([a-z0-9])|.|:|/|\\-)]+(.[(jpg)|(bmp)|(gif)|(png)])";
    Pattern pattern = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(content);
    List<String> urls = new ArrayList<String>();
    while (matcher.find()) {
      urls.add(matcher.group());
    }
    return urls;
  }
}
